# user_role_dao.py
from sqlalchemy.orm import Session
from ..do.user_role_do import UserRoleModel


def getUserRole(db: Session, user_id: str, role_id: str):
    return db.query(UserRoleModel).filter(UserRoleModel.user_id == user_id, UserRoleModel.role_id == role_id).first()


def getUserRoles(db: Session, skip: int = 0, limit: int = 100):
    return db.query(UserRoleModel).offset(skip).limit(limit).all()


def createUserRole(db: Session, user_role):
    userRoleModel = UserRoleModel(**user_role)
    db.add(userRoleModel)
    db.commit()
    db.refresh(userRoleModel)
    return userRoleModel


def deleteUserRole(db: Session, user_id: str, role_id: str):
    userRoleModel = getUserRole(db, user_id, role_id)
    if userRoleModel is None:
        return False
    db.delete(userRoleModel)
    db.commit()
    return True

def delete_user_roles_by_role_id(db: Session, role_id: str):
    db.query(UserRoleModel).filter(UserRoleModel.role_id == role_id).delete()
    db.commit()
    return True

def delete_user_roles_by_user_id(db: Session, user_id: str):
    db.query(UserRoleModel).filter(UserRoleModel.user_id == user_id).delete()
    db.commit()
    return True

def get_roles_by_user_id(db: Session, user_id: str):
    return db.query(UserRoleModel).filter(UserRoleModel.user_id == user_id).all()
